package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zsk
 * @create 2022-02-18-16:28
 */
@RestController
public class CartController {


    //依赖注入
    @Autowired
    private CartService cartService;

    /*
    添加购物车
    Request URL: http://localhost:8081/cart/add
    Request Method: POST
     */
    @PostMapping("/cart/add")
    public ResultInfo save(@RequestBody Cart cartParam){
        //调用service层添加
        Cart cart = cartService.save(cartParam);
        //返回前端数据(主要需要success中的code值)
        return ResultInfo.success(cart);
    }

    /*
    查询购物车
    Request URL: http://localhost:8081/cart/list
    Request Method: GET
     */
    @GetMapping("/cart/list")
    public ResultInfo findList(){

        //调用service层查询数据
        List<Cart> cartList = cartService.findList();

        //返回结果
        return ResultInfo.success(cartList);

    }

    /*
    删除购物车中数据
    Request URL: http://localhost:8081/cart/clean
    Request Method: DELETE
     */
    @DeleteMapping("/cart/clean")
    public ResultInfo clean(){

        //调用service层删除
        cartService.clean();

        //返回结果
        return ResultInfo.success(null);

    }

    /*
    修改数据
    Request URL: http://localhost:8081/cart/sub
    Request Method: POST
     */
    @PostMapping("/cart/sub")
    public ResultInfo update(@RequestBody Cart cartId){

        //调用service修改
        Cart cart = cartService.update(cartId);
        //返回结果
        return ResultInfo.success(cart);
    }
}
